/**
 * 
 */
package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Member;
import com.jxc.model.po.PointUsedRecord;
import com.jxc.model.po.Salelist;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VPointUsedRecord;
import com.jxc.service.PointUsedRecordServiceI;

/**
 * @author Tino
 * 
 */
@Service("pointUsedRecordService")
public class PointUsedRecordServiceImpl implements PointUsedRecordServiceI {

	@Autowired
	private BaseDaoI<PointUsedRecord> pointUsedRecordDao;

	@Override
	public List<VPointUsedRecord> combobox(VPointUsedRecord vpur) {
		return this.changeModel(this.find(vpur));
	}

	@Override
	public DataGrid datagrid(VPointUsedRecord vpur) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vpur)));
		dg.setTotal(this.total(vpur));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					PointUsedRecord pur = this.pointUsedRecordDao.get(PointUsedRecord.class, Long.parseLong(id.trim()));
					if (pur != null) {
						this.pointUsedRecordDao.delete(pur);
					}
				}
			}
		}
	}

	@Override
	public void save(VPointUsedRecord vpur) {
		PointUsedRecord pur = new PointUsedRecord();
		BeanUtils.copyProperties(vpur, pur);
		if (vpur.getMid() != null) {
			pur.setMember(new Member(vpur.getMid()));
		}
		if (vpur.getSlid() != null) {
			pur.setSalelist(new Salelist(vpur.getSlid()));
		}
		this.pointUsedRecordDao.save(pur);
	}

	@Override
	public void update(VPointUsedRecord vpur) {
		PointUsedRecord pur = this.pointUsedRecordDao.get(PointUsedRecord.class, vpur.getId());
		BeanUtils.copyProperties(vpur, pur, new String[] { "id" });
		if (vpur.getMid() != null) {
			pur.setMember(new Member(vpur.getMid()));
		}
		if (vpur.getSlid() != null) {
			pur.setSalelist(new Salelist(vpur.getSlid()));
		}
		this.pointUsedRecordDao.update(pur);
	}

	private List<PointUsedRecord> find(VPointUsedRecord vpur) {
		String hql = "from PointUsedRecord t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vpur, hql, values);

		if (vpur.getSort() != null && vpur.getOrder() != null) {
			hql += " order by " + vpur.getSort() + " " + vpur.getOrder();
		}
		return this.pointUsedRecordDao.find(hql, values, vpur.getPage(), vpur.getRows());
	}

	private List<VPointUsedRecord> changeModel(List<PointUsedRecord> purs) {
		List<VPointUsedRecord> vpurs = new ArrayList<VPointUsedRecord>();
		if (vpurs != null && vpurs.size() > 0) {
			for (PointUsedRecord pur : purs) {
				VPointUsedRecord vpur = new VPointUsedRecord();
				BeanUtils.copyProperties(pur, vpur);
				if (pur.getMember() != null) {
					Member member = pur.getMember();
					vpur.setMid(member.getId());
					vpur.setMname(member.getName());
				}
				if (pur.getSalelist() != null) {
					Salelist sl = pur.getSalelist();
					vpur.setSlid(sl.getId());
					vpur.setSserial(sl.getSerial());
				}
				vpurs.add(vpur);
			}
		}
		return vpurs;
	}

	private Long total(VPointUsedRecord vpur) {
		String hql = "select count(*) from PointUsedRecord t where 1=1  ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vpur, hql, values);
		return this.pointUsedRecordDao.count(hql, values);
	}

	private String addWhere(VPointUsedRecord vpur, String hql, List<Object> values) {
		if (vpur.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vpur.getId());
		}
		if (vpur.getMid() != null) {
			hql += " and t.member.id = ? ";
			values.add(vpur.getMid());
		}
		if (vpur.getSlid() != null) {
			hql += " and t.salelist.id = ? ";
			values.add(vpur.getSlid());
		}
		return hql;
	}

}
